package me.xplabs.model.combatunit 
{
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	import me.xplabs.battle.buff.Buff;
	import me.xplabs.constant.BuffStatus;
	import me.xplabs.Globals;
	import me.xplabs.model.GameDataSingleton;
	import me.xplabs.model.msg.MsgAddBuffActor;
	import me.xplabs.model.msg.MsgDelBuffActor;
	import me.xplabs.model.RolePositionManager;
	import me.xplabs.model.skill.BaseSkill;
	import me.xplabs.model.skill.MSkillInfo;
	import me.xplabs.model.skill.Skill;
	import me.xplabs.model.skill.SkillManager;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.utils.MacroObjPool;
	import me.xplabs.utils.Utils;
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class MFlagInfo extends BaseFlag 
	{
		private var _hasSkills:Vector.<BaseSkill>;
		private var _timeKey:int;
		public var curPosX:int;
		public var curPosY:int;
		public var cellX:int;
		public var cellY:int;
		public var soleId:int;
		public var skillManager:SkillManager;
		/** 攻击遍历范围内的角色引用*/
		private var _roles:Vector.<MRoleInfo>;
		/** 走出遍历攻击范围的角色引用*/
		private var _changeRoles:Vector.<MRoleInfo>;
		/** 在攻击范围内的角色*/
		private var _areaRoles:Vector.<MRoleInfo>;
		private var _now:uint;
		private var _total:uint;
		private var _skills:Vector.<MSkillInfo>;
		/** 父级的id*/
		public var fatherSoleId:int;
		public function MFlagInfo() 
		{
			skillManager = new SkillManager();
			skillManager.useSkillCall = useSkillHandler;
			_timeKey = -1;
			
			_roles = new Vector.<MRoleInfo>();
			_changeRoles = new Vector.<MRoleInfo>();
			_areaRoles = new Vector.<MRoleInfo>();
			_skills = new Vector.<MSkillInfo>();
			_hasSkills = new Vector.<BaseSkill>();
		}
		
		private function useSkillHandler(p_skill:Skill):Boolean
		{
			updateRole();
			var len:int = _roles.length;
			if (len <= 0) return false;
			_areaRoles.length = 0;
			for (var i:int = 0; i < len; i++) 
			{
				var curRole:MRoleInfo = _roles[i];
				var ax:int = curRole.curPosX - curPosX;
				var ay:int = curRole.curPosY - curPosY;
				if (Math.abs(ax) > areaW || Math.abs(ay) > areaH) continue;
				if (Utils.checkPointInEllipse(ax, ay, areaW, areaH))
				{
					_areaRoles.push(_roles[i]);
				}
			}
			len = _areaRoles.length;
			if (len <= 0) return false;
			/*curRole = _areaRoles[0];
			for (i = 1; i < len; i++) 
			{
				if (_areaRoles[i].curLife < curRole.curLife)
				{
					curRole = _areaRoles[i];
				}
			}*/
			//trace("旗帜使用技能");
			
			len = p_skill.skillTypes.length;
			for (i = 0; i < len; i++) 
			{
				var roleLen:int = _areaRoles.length;
				for (var j:int = 0; j < roleLen; j++) 
				{
					var baseBuff:BaseBuff = GameDataSingleton.i.getBaseBuffById(p_skill.skillTypes[i]);
					if (!baseBuff) return false;
					var status:int = _areaRoles[j].checkBuff(baseBuff.baseId, baseBuff.level);
					switch(status)
					{
						case BuffStatus.NO_BUFF:
							sendMsg(NotificationName.FIGHT_MAP_MANAGER, new MsgAddBuffActor(_areaRoles[j].soleId, p_skill.skillTypes[i]), NotificationFight.ADD_BUFF);
							break;
						case BuffStatus.SAME_BUFF:
							break;
						case BuffStatus.REPLACE_BUFF:
							sendMsg(NotificationName.FIGHT_MAP_MANAGER, new MsgDelBuffActor(_areaRoles[j].soleId, baseBuff.baseId), NotificationFight.DEL_BUFF);
							sendMsg(NotificationName.FIGHT_MAP_MANAGER, new MsgAddBuffActor(_areaRoles[j].soleId, p_skill.skillTypes[i]), NotificationFight.ADD_BUFF);
							break;
					}
				}
			}
			return true;
		}
			/**
		 * 更新范围内的所有敌人引用
		 */
		private function updateRole():void
		{
			var prevRoles:Vector.<MRoleInfo> = _roles.slice();
			_changeRoles.length = 0;
			
			
			_roles.length = 0;
			var paths:Dictionary = RolePositionManager.i.paths;
			if (paths)
			{
				var startX:int = cellX - areaCellW;
				var startY:int = cellY - areaCellH;
				var endX:int = cellX + areaCellW;
				var endY:int = cellY + areaCellH;
				for (var i:int = startX; i <= endX; i++)
				{
					for (var j:int = startY; j <= endY; j++)
					{
						var temp:Vector.<MRoleInfo> = paths[i + "-" + j];
						if (temp && temp.length > 0)
						{
							var len:int = temp.length;
							for (var k:int = 0; k < len; k++) 
							{
								_roles.push(temp[k]);
								//trace("被添加进的角色id" + temp[k].soleId);
							}
						}
					}
				}
			}
			//_roles.sort(sortRole);
			findDifferent(prevRoles, _roles);
		}
		/**
		 * 查找走出攻击范围的角色
		 * @param	prevRoles
		 * @param	roles
		 */
		private function findDifferent(prevRoles:Vector.<MRoleInfo>, roles:Vector.<MRoleInfo>):void
		{
			var obj:Object = { };
			var len:int = roles.length;
			for (var i:int = 0; i < len; i++) 
			{
				obj[roles[i].soleId] = true;
			}
			len = prevRoles.length;
			for (i = 0; i < len; i++) 
			{
				if (!obj[prevRoles[i]])
				{
					_changeRoles.push(prevRoles[i]);
				}
			}
		}
		public function heartbeat():void 
		{
			var prev:uint = getTimer() - _now;
			_now = getTimer();
			_total += prev;
			var gap:uint = _total / 300;
			if (gap >= 1)
			{
				_total -= gap * 300;
				updateRole();
			}
			//updateRole();
			skillManager.heartbeat(0);
			/*var len:int = _skills.length;
			for (var i:int = len - 1; i >= 0; i--)
			{
				if (_skills[i].heartbeat(prev))
				{
					removeSkill(_skills.splice(i, 1)[0]);
				}
			}*/
		}
		/*private function removeSkill(p_mSkillInfo:MSkillInfo):void
		{
			Globals.delSoleId(p_mSkillInfo.soleId);
			sendMsg(NotificationName.FIGHT_NOTIFICATION, p_mSkillInfo.soleId, NotificationFight.REMOVE_HIT_EFFECT);
			var tempHit:int = p_mSkillInfo.hit;
			var tempId:int = p_mSkillInfo.attackRoleId
			p_mSkillInfo.dispose();
			MacroObjPool.dispose(p_mSkillInfo);
			p_mSkillInfo = null;
			//curRole.life = Math.max(0, curRole.life-p_mSkillInfo.hit);
			sendMsg(NotificationName.FIGHT_MAP_MANAGER, new MsgHitActor(tempId, tempHit), NotificationFight.HIT_ACTOR);
		}*/
		public function initSkillList(p_skills:Vector.<BaseSkill>):void 
		{
			skillManager.clearSkills();
			if (_timeKey != -1)
			{
				Globals.timeManager.removeGap(_timeKey);
				_timeKey = -1;
			}
			if (!p_skills) return;
			_hasSkills = p_skills;
			_timeKey = Globals.timeManager.addDelay(1,delayInitSkills);
		}
		
		private function delayInitSkills():void 
		{
			_timeKey = -1;
			if (!_hasSkills) return;
			var len:int = _hasSkills.length;
			for (var i:int = 0; i < len; i++) 
			{
				var p_skill:BaseSkill = _hasSkills[i];
				if (!p_skill) continue;
				var tempSkill:Skill = MacroObjPool.getObj(Skill, 1, 1);
				tempSkill.title = p_skill.title;
				tempSkill.baseId = p_skill.baseId;
				tempSkill.skillId = p_skill.skillId;
				tempSkill.resId = p_skill.resId;
				tempSkill.area = areaW;
				tempSkill.describe = p_skill.describe;
				tempSkill.cdGap = p_skill.cdGap;
				tempSkill.hitId = p_skill.hitId;
				tempSkill.injuredId = p_skill.injuredId;
				tempSkill.isAOE = p_skill.isAOE;
				tempSkill.isPath = p_skill.isPath;
				tempSkill.skillTypes = p_skill.skillTypes.slice();
				tempSkill.gravity = p_skill.gravity;
				tempSkill.hit = p_skill.hit;
				tempSkill.speed = p_skill.speed;
				tempSkill.magic = p_skill.magic;
				tempSkill.magicType = p_skill.magicType;
				tempSkill.gravity = p_skill.gravity;
				skillManager.addSkill(tempSkill);
			}
		}
		public function dispose():void
		{
			
		}
	}

}















